
<!DOCTYPE html>
<html lang="en" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>Rest风格开发 - Hexo</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="Fechin,"> 
    <meta name="description" content="2. Rest开发2.1 REST简介
REST(Representational State Transfer),表现形式状态转换（访问网络资源的格式）


传统风格资源访问形式
http://l,"> 
    <meta name="author" content="John Doe"> 
    <link rel="alternative" href="atom.xml" title="Hexo" type="application/atom+xml"> 
    <link rel="icon" href="/yi-mark-gitee-io/img/favicon.png"> 
    
    
    
    <meta name="twitter:card" content="summary"/>
    <meta name="twitter:title" content="Rest风格开发 - Hexo"/>
    <meta name="twitter:description" content="2. Rest开发2.1 REST简介
REST(Representational State Transfer),表现形式状态转换（访问网络资源的格式）


传统风格资源访问形式
http://l,"/>
    
    
    
    
    <meta property="og:site_name" content="Hexo"/>
    <meta property="og:type" content="object"/>
    <meta property="og:title" content="Rest风格开发 - Hexo"/>
    <meta property="og:description" content="2. Rest开发2.1 REST简介
REST(Representational State Transfer),表现形式状态转换（访问网络资源的格式）


传统风格资源访问形式
http://l,"/>
    
<link rel="stylesheet" href="/yi-mark-gitee-io/css/diaspora.css">

<meta name="generator" content="Hexo 6.0.0"></head>

<body class="loading">
    <span id="config-title" style="display:none">Hexo</span>
    <div id="loader"></div>
    <div id="single">
    <div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <a class="iconfont icon-home image-icon" href="javascript:;" data-url="https://yi-mark.gitee.io"></a>
    <div title="播放/暂停" class="iconfont icon-play"></div>
    <h3 class="subtitle">Rest风格开发</h3>
    <div class="social">
        <div>
            <div class="share">
                <a title="获取二维码" class="iconfont icon-scan" href="javascript:;"></a>
            </div>
            <div id="qr"></div>
        </div>
    </div>
    <div class="scrollbar"></div>
</div>

    <div class="section">
        <div class="article">
    <div class='main'>
        <h1 class="title">Rest风格开发</h1>
        <div class="stuff">
            <span>二月 03, 2022</span>
            
  <ul class="post-tags-list" itemprop="keywords"><li class="post-tags-list-item"><a class="post-tags-list-link" href="/yi-mark-gitee-io/tags/Rest/" rel="tag">Rest</a></li><li class="post-tags-list-item"><a class="post-tags-list-link" href="/yi-mark-gitee-io/tags/SpringBoot/" rel="tag">SpringBoot</a></li></ul>


        </div>
        <div class="content markdown">
            <h1 id="2-Rest开发"><a href="#2-Rest开发" class="headerlink" title="2. Rest开发"></a>2. Rest开发</h1><h2 id="2-1-REST简介"><a href="#2-1-REST简介" class="headerlink" title="2.1 REST简介"></a>2.1 REST简介</h2><blockquote>
<p>REST(Representational State Transfer),表现形式状态转换（访问网络资源的格式）</p>
</blockquote>
<ul>
<li>传统风格资源访问形式<ul>
<li><a target="_blank" rel="noopener" href="http://localhost/user/getById?id=1">http://localhost/user/getById?id=1</a></li>
<li><a target="_blank" rel="noopener" href="http://localhost/user/saveUser">http://localhost/user/saveUser</a></li>
</ul>
</li>
<li>REST风格描述形式<ul>
<li><a target="_blank" rel="noopener" href="http://localhost/user/1">http://localhost/user/1</a></li>
<li><a target="_blank" rel="noopener" href="http://localhost/user">http://localhost/user</a></li>
</ul>
</li>
</ul>
<blockquote>
<p>优点</p>
</blockquote>
<ul>
<li>书写简单</li>
<li>隐藏资源访问的行为，无法通过地址得知对资源是何种操作</li>
</ul>
<blockquote>
<p>按照REST风格访问资源时使用行为动作区分对资源进行了何种操作</p>
</blockquote>
<p><img src="https://s2.loli.net/2022/02/03/hxgYlHJdcB5CAOa.png" alt="image-20220203222944956"></p>
<blockquote>
<p>根据REST风格对资源进行访问称为Restful</p>
<ul>
<li>注意事项</li>
</ul>
<p><strong>上述行为是约定方式，约定不是规范，可以打破，所以称REST风格，而不是REST规范。描述模块的名称通常使用复数，也就是加s的格式描述，表示此类资源，而非当个资源</strong></p>
</blockquote>
<h2 id="2-2-实战开发"><a href="#2-2-实战开发" class="headerlink" title="2.2 实战开发"></a>2.2 实战开发</h2><blockquote>
<p>示例1</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@ResponseBody</span></span><br><span class="line"><span class="meta">@RequestMapping(value = &quot;/users&quot;,method = RequestMethod.POST)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">save</span><span class="params">()</span></span>&#123;</span><br><span class="line">    System.out.println(<span class="string">&quot;user save...&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;hello World&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<blockquote>
<p>示例2</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@ResponseBody</span></span><br><span class="line"><span class="meta">@RequestMapping(value = &quot;/users/&#123;id&#125;&quot;,method = RequestMethod.DELETE)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">delete</span><span class="params">(<span class="meta">@PathVariable</span> Integer id)</span></span>&#123;</span><br><span class="line">    System.out.println(<span class="string">&quot;user delete...&quot;</span>+id);</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;hello hzy&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>通过@PathVariable告诉变量来自路径</li>
<li>通过value &#x3D; “&#x2F;users&#x2F;{id}” 代表从路径中取值，{}中是形参的名字</li>
</ul>
<h2 id="2-3-快速开发"><a href="#2-3-快速开发" class="headerlink" title="2.3 快速开发"></a>2.3 快速开发</h2><blockquote>
<p>在下面示例代码中，部分代码注解可以合并简化</p>
</blockquote>
<p><img src="https://s2.loli.net/2022/02/03/LdVXcsHNIb9ph7P.png" alt="image-20220203223615747"></p>
<blockquote>
<p>简化后</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RestController</span> <span class="comment">// 合并了@Controller和@ResponseBody</span></span><br><span class="line"><span class="meta">@RequestMapping(&quot;/users&quot;)</span> <span class="comment">// 路径，rest风格路径名通常为复数</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserController</span> </span>&#123;</span><br><span class="line">    <span class="meta">@PostMapping</span> <span class="comment">// 表示访问行为动作</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">save</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;user save...&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;hello World&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@DeleteMapping(&quot;/&#123;id&#125;&quot;)</span> <span class="comment">// 表示路径参数</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">delete</span><span class="params">(<span class="meta">@PathVariable</span> Integer id)</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;user delete...&quot;</span>+id);</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;hello yihen&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="2-4-注解解析"><a href="#2-4-注解解析" class="headerlink" title="2.4 注解解析"></a>2.4 注解解析</h2><p><img src="https://s2.loli.net/2022/02/03/t7TWqGmwRNCzILU.png" alt="image-20220203224128200"></p>
<p><img src="https://s2.loli.net/2022/02/03/pCiE7jGb2fROweY.png" alt="image-20220203224146618"></p>
<h2 id="2-5-总结"><a href="#2-5-总结" class="headerlink" title="2.5 总结"></a>2.5 总结</h2><blockquote>
<p>@RequestBody、@RequestParam、@PathVariable</p>
</blockquote>
<ul>
<li><p>区别</p>
<ul>
<li><p>@RequestParam用于接收url地址传参或表单传参</p>
</li>
<li><p>@RequestBody用于接收json数据</p>
</li>
<li><p>@PathVariable用于接收路径参数，使用{参数名称}描述路径参数</p>
</li>
</ul>
</li>
<li><p>应用</p>
<ul>
<li><p>后期开发中，发送请求参数超过一个时，以json格式为主，@RequestBody应用较广</p>
</li>
<li><p>如果发送非json格式数据，选用@RequestParam接收请求参数</p>
</li>
<li><p>采用RESTful进行开发，当参数数量较少时，例如1个，可以采用@PathVariable接收请求路径变量，通常传递id值</p>
</li>
</ul>
</li>
</ul>

            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls" data-autoplay="false">
                <source type="audio/mpeg" src="">
            </audio>
            
                <ul id="audio-list" style="display:none">
                    
                        
                            <li title="0" data-url="http://link.hhtjim.com/163/425570952.mp3"></li>
                        
                    
                        
                            <li title="1" data-url="http://link.hhtjim.com/163/425570952.mp3"></li>
                        
                    
                </ul>
            
        </div>
        
    <div id="gitalk-container" class="comment link"
		data-enable="false"
        data-ae="false"
        data-ci=""
        data-cs=""
        data-r=""
        data-o=""
        data-a=""
        data-d="false"
    >查看评论</div>


    </div>
    
</div>


    </div>
</div>
</body>


<script src="//lib.baomitu.com/jquery/1.8.3/jquery.min.js"></script>
<script src="/yi-mark-gitee-io/js/plugin.js"></script>
<script src="/yi-mark-gitee-io/js/typed.js"></script>
<script src="/yi-mark-gitee-io/js/diaspora.js"></script>


<link rel="stylesheet" href="/yi-mark-gitee-io/photoswipe/photoswipe.css">
<link rel="stylesheet" href="/yi-mark-gitee-io/photoswipe/default-skin/default-skin.css">


<script src="/yi-mark-gitee-io/photoswipe/photoswipe.min.js"></script>
<script src="/yi-mark-gitee-io/photoswipe/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>






</html>
